<?php
/**
 * Contains function for the framework management, like imports ecc...
 *
 * @author Matteo Vigoni <mattevigo@gmail.com>
 *
 * @package DBEntity
 * @version 0.2
 */
require_once $_SERVER['DOCUMENT_ROOT']."/config_dbentity.php";
/**
 * Definition of the FrameworkException for the handling of errors
 *
 */
class FrameworkException extends Exception
{
	function __construct($message){
		parent::__construct($message);
	}
}

/**
 * Import php file, for example if you need to import the file ./includes/my_file.php, call this function:
 * 
 * import(includes.my_file);
 * 
 * @param $path
 */
function import($path)
{
	$new_path = $_SERVER['DOCUMENT_ROOT'] . "/" . str_replace(".", "/", $path) . ".php";
	
	require_once $new_path;
}

/**
 * Redirect the page to the target script_name
 * 
 * @param $script_name
 * @return unknown_type
 */
function script( $script_name=null, $content_type="application/x-javascript" )
{
	$script_path = SCRIPTS . "/" . $script_name . ".php";
	
	if(!file_exists($script_path)) throw new FrameworkException("$script_path: file doesn't exist");
	
	header("content-type: ".$content_type);
	require_once SCRIPTS . "/" . $script_name . ".php";
}

/**
 * Redirect the page to the target script_name
 * 
 * @param $script_name
 * @return unknown_type
 */
function page($page_name=null, $content_type="text/html")
{
	$page_path = PAGES . "/" . $page_name . ".php";
	
	if(!file_exists($page_path)) throw new FrameworkException("$page_path: file doesn't exist");
	
	header("content-type: ".$content_type);
	require_once $page_path;
}

function getDB($session_time=SESSION_TIME)
{
	global $db_config;
	return new DB(&$db_config, $session_time);
}
?>